package com.qsd.TeacherSystem.Service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qsd.TeacherSystem.Service.GradeService;
import com.qsd.TeacherSystem.dao.AnswerDao;
import com.qsd.TeacherSystem.dao.GradeDao;
import com.qsd.common.enums.ResultStatus;
import com.qsd.common.po.Answer;
import com.qsd.common.po.Grade;
import com.qsd.common.util.PageUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class GradeServiceImpl implements GradeService {

    @Resource
    private AnswerDao answerDao;
    @Resource
    private GradeDao gradeDao;

    @Override
    public ResultStatus subGrade(Integer answerId, Integer grade) {
        //查询答案表
        Answer answer = answerDao.queryById(answerId);
        //构建成绩表
        Grade grades = new Grade(answer);
        grades.setGrade(grade);
        //插入成绩表
        int saveGrade = gradeDao.saveGrade(grades);
        //删除答案表
        if (saveGrade > 0) {
            answerDao.deleteAnswerById(answerId);
            return ResultStatus.SUCCESS;
        }
        return ResultStatus.SYSTEM_ERROR;
    }


    @Override
    public List<Grade> fileList(Grade grade, PageUtil page) {
        IPage<Grade> andPage = gradeDao.findAndPage(new Page<>(page.getPage(),page.getLimit()), grade);
        page.setCount((int) andPage.getTotal());
        return andPage.getRecords();
    }
}
